package view.component.sider
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.events.MouseEvent;

import framework.proxy.ComponentProxy;

/**
 * SiderItemUIElementProxy
 * @author jliang
 * 
 */
public class SiderItemUIElementProxy extends ComponentProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function SiderItemUIElementProxy()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	public var clickCallBack:Function;
	
	public var mouseOverCallBack:Function;
	
	public var mouseOutCallBack:Function;
    
	public var data:* = {};
	
	private var _content:DisplayObject;

	public function get content():DisplayObject
	{
		return _content;
	}
	
	private var _isWorking:Boolean = false;

	public function get isWorking():Boolean
	{
		return _isWorking;
	}

	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
		skin.mouseChildren = false;
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function add(displayObj:DisplayObject):void
	{
		removeChildren();
		
		setupListener(skin);
		
		skin.mouseEnabled = skin.buttonMode = true;
		
		skin.addChild(displayObj);
		
		_isWorking = true;
		_content = displayObj;
	}
	
	public function remove():void
	{
		removeChildren();
		
		clearListener(skin);
		
		skin.mouseEnabled = false;
		
		data = {};
		clickCallBack = null;
		mouseOverCallBack = null;
		mouseOutCallBack = null;
		_content = null;
		
		_isWorking = false;
	}
	
	private function removeChildren():void
	{
		while (skin.numChildren > 0)
		{
			skin.removeChildAt(0);
		}
	}
	//==========================================================================
	//  Private methods
	//==========================================================================
	private function setupListener(target:IEventDispatcher):void
	{
		target.addEventListener(MouseEvent.CLICK, mouseClickHandler);
		target.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
		target.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
	}
	
	private function clearListener(target:IEventDispatcher):void
	{
		target.removeEventListener(MouseEvent.CLICK, mouseClickHandler);
		target.removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
		target.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================	
	private function mouseClickHandler(event:Event):void
	{
		if (clickCallBack != null)
		{
			clickCallBack(data);
		}
	}
	
	private function mouseOverHandler(event:Event):void
	{
		if (mouseOverCallBack != null)
		{
			mouseOverCallBack(data);
		}
	}
	
	private function mouseOutHandler(event:Event):void
	{
		if (mouseOutCallBack != null)
		{
			mouseOutCallBack(data);
		}
	}
}
}